﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Micrhard.FMCG.Server.Interface;
using Micrhard.FMCG.Server.Models;
using Micrhard.FMCG.Server.DataAccess;
using Micrhard.FMCG.Server.DTO;
using Microhard.Core;
using Microhard.Core.DBUtility;

namespace Micrhard.FMCG.Server.Impl
{
    public class NewsDistinct : IEqualityComparer<NewsModel>
    {

        public bool Equals(NewsModel x, NewsModel y)
        {
            if (x == null)
                return false;
            if (y == null)
                return false;
            if (x.DetailLink == y.DetailLink)
                return true;
            return false;
        }

        public int GetHashCode(NewsModel obj)
        {
            return obj.DetailLink.GetHashCode();
        }
    }

    public class NewsImpl : INews
    {
        public List<Models.NewsModel> GetNew(List<Models.NewsModel> news)
        {
            if (news.Count == 0)
                return new List<NewsModel>();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            //过滤重复
            news = news.Distinct(new NewsDistinct()).ToList();
            List<NewsModel> notExist;
            //获得不存在的新闻
            if (db.News.Count() == 0)
            {
                notExist = news;
            }
            else
            {
                notExist = new List<NewsModel>();
                foreach (var n in news)
                {
                    int cnt = db.News.Count(x => x.DetailLink == n.DetailLink);
                    if (cnt == 0)
                    {
                        notExist.Add(n);
                    }
                }
            }
            //插入到数据库中
            if (notExist.Count > 0)
            {
                notExist.ForEach(i =>
                {
                    i.NewsID = Guid.NewGuid();
                    var entity = i.ToEntity();
                    entity.CreateDate = DateTime.Now;
                    entity.PublishDate = DateTime.Now;
                    entity.CompanyID = i.CompanyID;
                    db.AddToNews(entity);
                });
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    Log.Error("INews", ex);
                }
            }
            return notExist;
        }

        public Microhard.Core.DBUtility.ExecutedResult UpdateNewsDetail(List<Models.NewsModel> news)
        {
            ExecutedResult rtn = new ExecutedResult();
            if (news.Count == 0)
                return rtn;
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();

            foreach (var d in news)
            {
                var entity = db.News.SingleOrDefault(n => n.NewsID == d.NewsID);
                entity.Detail = d.Detail;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error("UpdateNewsDetail", ex);
                rtn.ErrorMsg.Add("UpdateNewsDetailFailed", ex.Message);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Add(Models.NewsModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.NewsModel model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.NewsModel model)
        {
            throw new NotImplementedException();
        }

        public Models.NewsModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.NewsModel GetModelByModel(Models.NewsModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.NewsModel> Query(Models.NewsModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.NewsModel> Query(Models.NewsModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}
